//
//  GMAnalysisVC.m
//  Teecloud
//
//  Created by 小熊 on 2018/11/15.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMAnalysisVC.h"
#import "GMExamModel.h"
#import "GMSubjectsModel.h"
#import "GMAnalysisCell.h"
#import "GMResultVC.h"
#import "GMCommentVC.h"

@interface GMAnalysisVC ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) UIBarButtonItem *colectItem;
@property (nonatomic, strong) UIBarButtonItem *pinglunItem;
@property (nonatomic, strong) UIBarButtonItem *settingBtnItem;
@end

@implementation GMAnalysisVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"题目解析";
    self.view.backgroundColor = kWhiteColor;
    if (@available(iOS 11.0, *)) {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
//    [self getExaminationData];
    [self.collectionView registerNib:[UINib nibWithNibName:@"GMAnalysisCell" bundle:nil] forCellWithReuseIdentifier:@"GMAnalysisCell"];
    UIButton *pinglunBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [pinglunBtn addTarget:self action:@selector(commentBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [pinglunBtn setImage:[UIImage imageNamed:@"conment"] forState:UIControlStateNormal];
    [pinglunBtn sizeToFit];
    _pinglunItem = [[UIBarButtonItem alloc] initWithCustomView:pinglunBtn];
    UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingBtn addTarget:self action:@selector(jumpToCard) forControlEvents:UIControlEventTouchUpInside];
    [settingBtn setImage:[UIImage imageNamed:@"sumbit"] forState:UIControlStateNormal];
    [settingBtn sizeToFit];
    _settingBtnItem = [[UIBarButtonItem alloc] initWithCustomView:settingBtn];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatatabView) name:@"updata" object:nil];
    if (self.baseModel.IsAllowRemark) {
        self.navigationItem.rightBarButtonItems  = @[self.settingBtnItem,self.pinglunItem];
    }else{
        self.navigationItem.rightBarButtonItems  = @[self.settingBtnItem];
    }
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:self.baseModel.Subjects];
    [self.collectionView reloadData];
    if (self.index) {
          [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.index-1 inSection:0] atScrollPosition:(UICollectionViewScrollPositionRight) animated:YES];
    }
}
-(void)updatatabView{
    [self.collectionView reloadData];
}
-(void)getExaminationData{
    NSDictionary *dict= @{@"from":FROM_IOS,
                          @"busiOrgId":[XGMArchiverTools cacheForKey:BUSIORGID],
                          @"loginFinger":[XGMArchiverTools cacheForKey:USER_identifierNumber],
                          @"userId":[XGMArchiverTools cacheForKey:USER_UserId],
                          @"examId":self.examId,
                          };
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork HTTPWithMethod:HJRequestMethodPOST url:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=GetExamResult&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                GMExamModel *model = [GMExamModel mj_objectWithKeyValues:[responseObject objectForKey:RESULT_NAME_Data]];
                self.baseModel = model;
                if (model.IsAllowRemark) {
                    self.navigationItem.rightBarButtonItems  = @[self.settingBtnItem,self.pinglunItem];
                }else{
                    self.navigationItem.rightBarButtonItems  = @[self.settingBtnItem];
                }
                [self.dataArr removeAllObjects];
                [self.dataArr addObjectsFromArray:model.Subjects];
                [self.collectionView reloadData];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:self.view];
        }
    }];
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -35, DEVICE_WIDTH, self.view.frame.size.height) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.pagingEnabled = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GMAnalysisCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMAnalysisCell" forIndexPath:indexPath];
    GMSubjectsModel *subModel = self.dataArr[indexPath.row];
    cell.model = subModel;
    cell.name = self.baseModel.Title;
    cell.index = indexPath.row+1;
    cell.total = self.baseModel.Count;
    /**下一题*/
    cell.nextBlock = ^() {
        dispatch_async(dispatch_get_main_queue(), ^{
            [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0] atScrollPosition:(UICollectionViewScrollPositionRight) animated:YES];
        });
    };
    /**上一题*/
    cell.lastBlock = ^() {
        dispatch_async(dispatch_get_main_queue(), ^{
            [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:0] atScrollPosition:(UICollectionViewScrollPositionLeft) animated:YES];
        });
    };
    return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

//评论
-(void)commentBtnClick{
    GMCommentVC *comment = [[GMCommentVC alloc] init];
    comment.resId = self.baseModel.ResId;
    [self.navigationController pushViewController:comment animated:YES];
}
//跳转到答题卡
-(void)jumpToCard{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
